﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Common;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;
namespace ETL.Business.Handlers.ProjectActualResourceHandler
{
    public class UpdateActualResourceUsage
    {

        private readonly ProjectActualResourceUsageRepository _projectActualResourceUsageRepository;

        #region Constructor
        public UpdateActualResourceUsage(ProjectActualResourceUsageRepository projectActualResourceUsageRepository)
        {
            _projectActualResourceUsageRepository = projectActualResourceUsageRepository;
        }
        public UpdateActualResourceUsage()
        {
            _projectActualResourceUsageRepository = new ProjectActualResourceUsageRepository();
        }
        #endregion

        #region Methods
        public virtual void Execute(decimal genActualResourceUsageId, string projectId, string taskId, DateTime newEndDate, string userId)
        {
            ETL_ACTUAL_RESOURCE_USAGES projectActualResourceUsageData = null;

            // try to get from db
            try
            {
                // convert
                projectActualResourceUsageData =
                    _projectActualResourceUsageRepository.GetActualResourceUsage(genActualResourceUsageId);
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // Update project start date, status
            if (projectActualResourceUsageData != null)
            {
                projectActualResourceUsageData.END_DATE = newEndDate;
                projectActualResourceUsageData.CLOCKED_OFF_BY = userId;
                //projectActualResourceUsageData.t_status = Constants.ProjectStatusCode.IN_PROGRESS;

                _projectActualResourceUsageRepository.UpdateActualResourceUsage(projectActualResourceUsageData);
            }

        }

        public virtual void Execute(decimal genActualResourceUsageId, DateTime newStartDate, DateTime newEndDate)
        {
            ETL_ACTUAL_RESOURCE_USAGES projectActualResourceUsageData = null;

            // try to get from db
            try
            {
                // convert
                projectActualResourceUsageData =
                    _projectActualResourceUsageRepository.GetActualResourceUsage(genActualResourceUsageId);
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // Update project start date, status
            if (projectActualResourceUsageData != null)
            {
                projectActualResourceUsageData.END_DATE = newEndDate;
                projectActualResourceUsageData.START_DATE = newStartDate;
                _projectActualResourceUsageRepository.UpdateActualResourceUsage(projectActualResourceUsageData);
            }

        }

        public virtual void Execute(decimal genActualResourceUsageId, DateTime newStartDate)
        {
            ETL_ACTUAL_RESOURCE_USAGES projectActualResourceUsageData = null;

            // try to get from db
            try
            {
                // convert
                projectActualResourceUsageData =
                    _projectActualResourceUsageRepository.GetActualResourceUsage(genActualResourceUsageId);
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // Update project start date, status
            if (projectActualResourceUsageData != null && !projectActualResourceUsageData.START_DATE.HasValue)
            {
                projectActualResourceUsageData.START_DATE = newStartDate;
                //projectActualResourceUsageData.t_status = Constants.ProjectStatusCode.IN_PROGRESS;

                _projectActualResourceUsageRepository.UpdateActualResourceUsage(projectActualResourceUsageData);
            }

        }
        #endregion

    }
}
